package com.example.dzbsh.service;

import com.alibaba.fastjson.JSON;
import com.example.dzbsh.dao.JiashuMapper;
import com.example.dzbsh.dao.JingshangQingkuangMapper;
import com.example.dzbsh.dao.RenyuanMapper;
import com.example.dzbsh.model.Dept;
import com.example.dzbsh.util.CreateJSON;
import com.example.dzbsh.util.CreateWord;
import com.example.dzbsh.util.MD5;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class DaochuService {
    @Value("${userfilepath}")
    String USERFILEPATH;
    @Resource
    RenyuanMapper ddRenyuanMapper;

    @Resource
    JiashuMapper ddJiashuMapper;

    @Resource
    JiashuService ddJiashuService;

    @Resource
    ChaheQingkuangService ddChaheService;

    @Resource
    DeptService ddDeptService;

    @Resource
    JingshangQingkuangMapper ddJingshangMapper;
    @Resource
    ZhenbieQingkuangService ddZhenbieQingkuangService;

    // 导出人员及家属经商情况
    public void  DaochuJingshangQingkuang(HttpServletRequest request, HttpServletResponse response){
        // 创建tostring的map
        Map<String,Object> mmToJsonMap = new HashMap<String,Object>();
        HttpSession session = request.getSession();
        if (session == null || session.getAttribute("DeptId") == null) {
           error("尚未登录,获取单位信息失败",response);
        }
        String ppDeptId = session.getAttribute("DeptId").toString();
        List<Map<String, Object>> mmRenyuanList= ddRenyuanMapper.selectValid(); // 所有人员
        List<Map<String,Object>>  mmAllJiashuList = ddJiashuMapper.selectValid();  // 所有家属
        List<Map<String,Object>> mmJingshangQingkuangList = ddJingshangMapper.selectValid(); // 所有经商信息

        // 存放的文件路径及名称
        String userfilepath = USERFILEPATH;
        String mmFileName = UUID.randomUUID().toString()+".json";

        // 循环人员
        for (int i=0;i<mmRenyuanList.size();i++){

          // 获得人员id 创建一个用来存储该人员亲属从商的集合
          String renyaunID= mmRenyuanList.get(i).get("RENYUANID") == null ? "" :mmRenyuanList.get(i).get("RENYUANID").toString();
          String mmTRUENAME= mmRenyuanList.get(i).get("TRUENAME") == null ? "" :mmRenyuanList.get(i).get("TRUENAME").toString();
          String mmZHENGJIANHAOMA= mmRenyuanList.get(i).get("ZHENGJIANHAOMA") == null ? "" :mmRenyuanList.get(i).get("ZHENGJIANHAOMA").toString();
          String mmDEPTNAME= mmRenyuanList.get(i).get("DEPTNAME") == null ? "" :mmRenyuanList.get(i).get("DEPTNAME").toString();

          // 将字段转码重新存放
          mmRenyuanList.get(i).put("TRUENAME", MD5.convertMD5(mmTRUENAME));
          mmRenyuanList.get(i).put("ZHENGJIANHAOMA",MD5.convertMD5(mmZHENGJIANHAOMA));
          mmRenyuanList.get(i).put("DEPTNAME",MD5.convertMD5(mmDEPTNAME));

          // 用于存在人员list里的家属集合
          List<Map<String,Object>> mmSuoshuJiashu = new ArrayList<Map<String,Object>>();

          for(int x=0;x<mmAllJiashuList.size();x++){
              Map<String,Object> mmJiashu= mmAllJiashuList.get(x);

              // 获得这个家属的家属id和人员id 和上下级对比
              String mmRenyuanId = mmJiashu.get("RENYUANID")==null ?"":mmJiashu.get("RENYUANID").toString();
              String mmDangqianJiashuId = mmJiashu.get("JIASHUID")==null ?"":mmJiashu.get("JIASHUID").toString();
              // 该家属经商数据集合
              List<Map<String,Object>> mmSuoshuJingshang = new ArrayList<Map<String,Object>>();

              // 人员id相同认定为该人员家属 将该家属集合存放入
              if(renyaunID.equals(mmRenyuanId)){

                  // 循环经商数据
                  for(int j=0;j< mmJingshangQingkuangList.size();j++){
                      Map<String,Object> mmJingshangqingkuang= mmJingshangQingkuangList.get(j);
                      String mmJiashuid = mmJingshangqingkuang.get("JIASHUID")==null?"":mmJingshangqingkuang.get("JIASHUID").toString();
                      // 相同则存放入该人员的家属经商信息中
                      if (mmJiashuid.equals(mmDangqianJiashuId)){

                          //  放入集合中
                          mmSuoshuJingshang.add(mmJingshangqingkuang);
                      }
                  }
                  // 将所属经商集合放入当前的家属Map中
                  mmAllJiashuList.get(x).put("JINGSHANGLIST",mmSuoshuJingshang);

                  // 然后将当前的家属放入该人员的家属List中
                  mmSuoshuJiashu.add(mmAllJiashuList.get(x));

              } // 经商集合循环结束



           } // 家属集合循环结束

          // 将每个人的家属经商情况List 放入个人信息的map中
          mmRenyuanList.get(i).put("JIASHULIST",mmSuoshuJiashu);

        }
        mmToJsonMap.put("DEPTID",ppDeptId);
        mmToJsonMap.put("RENYUANLIST",mmRenyuanList);

        // 转化为Stirng
        String JsonString = JSON.toJSONString(mmToJsonMap);

        CreateJSON.createJson(JsonString,userfilepath,mmFileName,request,response);
    }

    // 导出委托查核经商办企业也信息汇总表
    public void createPaichuHuoxuandengji(String ppDeptId,HttpServletRequest request,
                                          HttpServletResponse response,
                                          ServletResponse servletResponse) {
        try {
            // 用于输出的Map
            Map<String, Object> mmResultMap = new HashMap<>();
            String mmJiashuNum =""; // 家属人数
            String mmDanweiName = ""; // 单位名称
            String mmDanweilianxiren =  "" ; // 单位联系人
            String mmDeptDianhua =  ""; // 单位电话
            String userfilepath = USERFILEPATH;


            // 获得该单位所有的家属
            List<Map<String,Object>> mmJiashuList = ddJiashuService.findBydeptId(ppDeptId);
            List<Map<String,Object>> mmDeptList = ddDeptService.findXiangxiByDeptId(ppDeptId);

            if(mmDeptList.size()<=0){
                error("未查询到该单位信息！",response);
            }else{

                mmDanweiName = mmDeptList.get(0).get("danweiduiwaimingcheng")==null?"":mmDeptList.get(0).get("danweiduiwaimingcheng").toString();
                mmDanweilianxiren = mmDeptList.get(0).get("truename")==null?"":mmDeptList.get(0).get("truename").toString();
                mmDeptDianhua = mmDeptList.get(0).get("phone")==null?"":mmDeptList.get(0).get("phone").toString();
            }

            List<Map<String, Object>> mmNewJiashuList = new ArrayList<Map<String,Object>>();

            for(int i = 0; i<mmJiashuList.size();i++){
                Map<String, Object> mmMap = new HashMap<String, Object>();
                String mmName= mmJiashuList.get(i).get("name") == null? "": mmJiashuList.get(i).get("name").toString();
                String mmShenfenzhenghao= mmJiashuList.get(i).get("shenfenzhenghao") == null? "": mmJiashuList.get(i).get("shenfenzhenghao").toString();

                mmMap.put("xuhao", i+1);
                mmMap.put("name", mmName);
                mmMap.put("zhengjianhaoma", mmShenfenzhenghao);
                mmNewJiashuList.add(mmMap);
            }

            /*mmResultMap.put("pingshengongzuo", mmPingshengongzuo);*/
            mmResultMap.put("num", mmJiashuList.size());
            mmResultMap.put("danweiname", mmDanweiName);
            mmResultMap.put("name", mmDanweilianxiren);
            mmResultMap.put("dianhua", mmDeptDianhua);

            mmResultMap.put("renyuanlist", mmNewJiashuList);
            String mmNewFileName= UUID.randomUUID().toString()+".xls";

            CreateWord.createXLS("",USERFILEPATH,mmNewFileName,"chaheqiyexinxihuizong.xml",mmResultMap,request,response);
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    // 导出查核情况
    public void  DaochuChaheQingkuang(String ppDeptId,boolean ppIsAll,HttpServletRequest request, HttpServletResponse response){
        // 创建tostring的map
        Map<String,Object> mmToJsonMap = new HashMap<String,Object>();
        List<Map<String,Object>> mmCahehAll = new ArrayList<Map<String,Object>>();

        HttpSession session = request.getSession();
        if (session == null || session.getAttribute("DeptId") == null) {
            error("尚未登录,获取单位信息失败",response);
        }

        List<String> mmDeptList  = new ArrayList<>();
        Dept mmDept = ddDeptService.findOne(ppDeptId);
        mmDeptList.add(ppDeptId);
        // 如果包含其 下属单位 则先查出所有单位信息。
        if(ppIsAll){
            mmDeptList.addAll(ddDeptService.findXiajiByDeptId(ppDeptId));
        }

        // 存放的文件路径及名称
        String userfilepath = USERFILEPATH;
        String mmFileName = "chahe"+UUID.randomUUID().toString()+".json";

        // 循环单位id集合
        for(int i =0;i<mmDeptList.size();i++){

            // 查核每个单位的 查核数据 并存入导出集合中
            List<Map<String,Object>> mmCaheList = ddChaheService.findByDeptId(mmDeptList.get(i));
            mmCahehAll.addAll(mmCaheList);
        }

        mmToJsonMap.put("DEPTID",ppDeptId);
        mmToJsonMap.put("JIGOUMINGCHENGMA",mmDept.getJigoumingchengma());
        mmToJsonMap.put("CHAHELIST",mmCahehAll);

        // 转化为Stirng
        String JsonString = JSON.toJSONString(mmToJsonMap);

        CreateJSON.createJson(JsonString,userfilepath,mmFileName,request,response);
    }

    // 导出甄别汇总表
    public void Zhenbiehuizong(String ppDeptId,String ppRenyuanLeibieId,String ppJigoumingchengma,String ppPaixu, HttpServletRequest request, HttpServletResponse response){
        try {
            // 存放的文件路径及名称
            String userfilepath = USERFILEPATH;
            // 获得单位名称
            String mmDeptName =" ";
            if(ppDeptId!=null && !ppDeptId.equals("")){
                Dept mmDept = ddDeptService.findOne(ppDeptId);
                mmDeptName = mmDept.getDanweiduiwaimingcheng();
            }

            // 获得领导列表
            List<Map<String,Object>> mmList = ddRenyuanMapper.findByDeptId(ppDeptId,ppRenyuanLeibieId,ppJigoumingchengma,ppPaixu);
            List<Map<String,Object>> mmReturnList = new ArrayList<>();
            Map<String,Object> mmReturnMap = new HashMap<>();

            // 循环领导信息
            for (int i = 0; i < mmList.size(); i++) {

                Map<String,Object> mmMap = mmList.get(i);
                Map<String,Object> mmRenturnRenyuanMap=new HashMap<>();
                if (mmMap!=null){
                    List<Map<String,Object>> mmReturnJiashuList = new ArrayList<>();

                    String mmRenyuanId = mmMap.get("renyuanid") == null ? "" : mmMap.get("renyuanid").toString();
                    String mmTruename = mmMap.get("truename") == null ? "" : mmMap.get("truename").toString();
                    String mmJigoumingchengma = mmMap.get("jigoumingchengma") == null ? "" : mmMap.get("jigoumingchengma").toString();
                    String mmBuzhibie = mmMap.get("buzhibie") == null ? "" : mmMap.get("buzhibie").toString();
                    String mmRenyuanleibie = mmMap.get("renyuanleibietext") == null ? "" : mmMap.get("renyuanleibietext").toString();

                    String mmChushengnianyue = mmMap.get("chusheng2") == null ? "" : mmMap.get("chusheng2").toString();
                    String mmJunxianleibie = mmMap.get("junxianleibietext") == null ? "" : mmMap.get("junxianleibietext").toString();
                    String mmJunxiandengji = mmMap.get("junxiandengjitext") == null ? "" : mmMap.get("junxiandengjitext").toString();
                    String mmJunxianshijian = mmMap.get("junxianshijian2") == null ? "" : mmMap.get("junxianshijian2").toString();
                    String mmZhiwucengji = mmMap.get("zhiwucengjitext") == null ? "" : mmMap.get("zhiwucengjitext").toString();
                    String mmZhiwushijian = mmMap.get("zhiwushijian2") == null ? "" : mmMap.get("zhiwushijian2").toString();
                    String mmShifoujingshang = mmMap.get("shifoujingshang") == null ? "" : mmMap.get("shifoujingshang").toString();
                    int hebingshu = 0;
                    int mmZhenbieNum = ddZhenbieQingkuangService.getCountByzhuangtaiAndRenyuanId(mmRenyuanId);
                    if (mmZhenbieNum>0){
                        hebingshu = mmZhenbieNum-1;
                    }

                    String mmHebing = "";


                    // 获得家属列表
                    List<Map<String,Object>> mmJiashuList = ddJiashuService.findByRenyuanid(mmRenyuanId);

                    String mmHuanhangfu = ""; // 换行符
                    if (mmJiashuList.size()==0){
                        mmHuanhangfu="</Row><Row ss:Height=\"36\"> ";

                    }

                    String mmTianchongge = "";
                    if (mmJiashuList.size()==0){   // 合并的行数要

                        mmTianchongge = "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell>";
                    }

                    if (mmZhenbieNum==0&&mmJiashuList.size()>0){
                        hebingshu = hebingshu-1;
                    }

                    for (int j = 0; j < mmJiashuList.size(); j++) {     // 循环家属信息
                        Map<String,Object> mmJiashuMap = mmJiashuList.get(j);
                        if (mmJiashuMap!=null){
                            List<Map<String,Object>> mmReturnZhenbieList = new ArrayList<>();
                            Map<String,Object> mmReturnJiashuMap = new HashMap<>();

                            String mmJiashuId = mmJiashuMap.get("jiashuid") == null ? "" : mmJiashuMap.get("jiashuid").toString();
                            String mmName = mmJiashuMap.get("name") == null ? "" : mmJiashuMap.get("name").toString();
                            String mmGuanxi = mmJiashuMap.get("guanxitext") == null ? "" : mmJiashuMap.get("guanxitext").toString();
                            String mmShenfenzhenghao = mmJiashuMap.get("shenfenzhenghao") == null ? "" : mmJiashuMap.get("shenfenzhenghao").toString();

                            List<Map<String,Object>> mmZhenbieqingkuangList = ddZhenbieQingkuangService.findByJiashuId(mmJiashuId); // 查询该家属的甄别情况
                            // 该家属存在甄别数据
                            String mmIsZhenbie = "否";
                            if(mmZhenbieqingkuangList.size()>0){
                                mmIsZhenbie= "是";
                            }
                            if(mmJiashuList.size()>1&&mmZhenbieqingkuangList.size()==0){
                                hebingshu= hebingshu+1;
                            }

                            String mmTianchongge2 = "";
                            if (mmZhenbieqingkuangList.size()==0){

                                mmTianchongge2 = "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                        "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                        "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                        "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                        "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                        "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                        "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                        "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                        "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                        "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                        "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                        "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell>";
                            }
                            String mmQiyeXingtai  =""; // 企业形态
                            for (int k = 0; k < mmZhenbieqingkuangList.size(); k++) {
                                Map<String,Object> mmJingshangMap = mmZhenbieqingkuangList.get(k);

                                if (mmJingshangMap!=null) {
                                    Map<String,Object> mmReturnZhenbieMap = new HashMap<>();
                                    String mmJINGSHANGJUTIXINGTAI = mmJingshangMap.get("jingshangjutixingtai") == null ? "" : mmJingshangMap.get("jingshangjutixingtai").toString(); // 企业形态
                                    String mmQiyename = mmJingshangMap.get("qiyename") == null ? "" : mmJingshangMap.get("qiyename").toString(); // 企业名称
                                    String mmQIYESUOZAIDI = mmJingshangMap.get("qiyesuozaidi") == null ? "" : mmJingshangMap.get("qiyesuozaidi").toString(); // 企业所在地
                                    String mmDENGJIZHUANGTAI = mmJingshangMap.get("dengjizhuangtai") == null ? "" : mmJingshangMap.get("dengjizhuangtai").toString(); // 登记状态
                                    String mmSHEHUIXINGYONGDAIMA = mmJingshangMap.get("shehuixinyongdaima") == null ? "" : mmJingshangMap.get("shehuixinyongdaima").toString(); // 社会信用代码
                                    String mmZHUCEZIBEN = mmJingshangMap.get("zhuceziben") == null ? "" : mmJingshangMap.get("zhuceziben").toString(); // 注册资本
                                    String mmRENJIAOCHUZIE = mmJingshangMap.get("renjiaochuzie") == null ? "" : mmJingshangMap.get("renjiaochuzie").toString(); // 认缴出资额
                                    String mmZHIWUTEXT = mmJingshangMap.get("zhiwutext") == null ? "" : mmJingshangMap.get("zhiwutext").toString(); // 职务
                                    String mmSHIFOUGUIFAN = mmJingshangMap.get("shifouguifan") == null ? "" : mmJingshangMap.get("shifouguifan").toString(); // 是否规范
                                    String mmBENRENTUICHU = mmJingshangMap.get("benrentuichu") == null ? "" : mmJingshangMap.get("benrentuichu").toString(); // 本人退出
                                    String mmTUICHUJINGSHANG = mmJingshangMap.get("tuichujingshang") == null ? "" : mmJingshangMap.get("tuichujingshang").toString(); // 退出经商企业办
                                    String mmBUGUIFANYUANYIN = mmJingshangMap.get("buguifanyuanyin") == null ? "" : mmJingshangMap.get("buguifanyuanyin").toString(); // 不规范原因
                                    String mmBEIZHU = mmJingshangMap.get("beizhu") == null ? "" : mmJingshangMap.get("beizhu").toString(); // 备注
                                    mmQiyeXingtai = mmQiyeXingtai+" "+mmJINGSHANGJUTIXINGTAI;
                                    if ("1".equals(mmSHIFOUGUIFAN)){
                                        mmSHIFOUGUIFAN = "是";
                                    }else{
                                        mmSHIFOUGUIFAN = "否";
                                    }

                                    String mmZhanwei = "";
                                    if (k>0){
                                        mmZhanwei =
                                                "<Cell ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell><Cell ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell><Cell ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell><Cell ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell><Cell ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell><Cell ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell><Cell ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell><Cell ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell><Cell ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell><Cell ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell><Cell ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell>";
                                    }
                                    String mmHuanhangfu2 = "";
                                    if (!((j-1)==mmJiashuList.size()&&(k-1)==mmZhenbieqingkuangList.size())){
                                        mmHuanhangfu2="</Row><Row ss:Height=\"36\"> ";
                                    }
                                    mmReturnZhenbieMap.put("zhanwei", mmZhanwei);
                                    mmReturnZhenbieMap.put("qiyename", mmQiyename);
                                    mmReturnZhenbieMap.put("qiyesuozaidizhi", mmQIYESUOZAIDI);
                                    mmReturnZhenbieMap.put("dengjizhuangtai", mmDENGJIZHUANGTAI);
                                    mmReturnZhenbieMap.put("shehuixingyongdaima", mmSHEHUIXINGYONGDAIMA);
                                    mmReturnZhenbieMap.put("zhuceziben", mmZHUCEZIBEN);
                                    mmReturnZhenbieMap.put("renjiaochuzie", mmRENJIAOCHUZIE);
                                    mmReturnZhenbieMap.put("zhiwu", mmZHIWUTEXT);
                                    mmReturnZhenbieMap.put("shifouguifan", mmSHIFOUGUIFAN);
                                    mmReturnZhenbieMap.put("benrentuichu", mmBENRENTUICHU);
                                    mmReturnZhenbieMap.put("tuichujingshang", mmTUICHUJINGSHANG);
                                    mmReturnZhenbieMap.put("buguifanyuanyin", mmBUGUIFANYUANYIN);
                                    mmReturnZhenbieMap.put("beizhu", mmBEIZHU);

                                    mmReturnZhenbieMap.put("huanhangfu", mmHuanhangfu2);

                                    mmReturnZhenbieList.add(mmReturnZhenbieMap);
                                }
                            }
                            String mmHebing2 = "";
                            if (mmReturnZhenbieList.size()>0){
                                int hebingshu2 = mmReturnZhenbieList.size()-1;
                                mmHebing2 = "ss:MergeDown=\""+hebingshu2+"\"";
                            }

                            String mmHuanhang = "";
                            if (mmReturnZhenbieList.size()==0){
                                mmHuanhang = "</Row><Row ss:Height=\"36\"> ";
                            }
                            String mmZhanwei = "";
                            if (j>0){
                                mmZhanwei="<Cell ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell><Cell ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell><Cell ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell><Cell ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell><Cell ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell><Cell ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell><Cell ss:StyleID=\"s53\"><Data ss:Type=\"String\"></Data></Cell>";
                            }

                            mmReturnJiashuMap.put("zhanwei",mmZhanwei);
                            mmReturnJiashuMap.put("tianchongge",mmTianchongge2);
                            mmReturnJiashuMap.put("name",mmName);
                            mmReturnJiashuMap.put("guanxi",mmGuanxi);
                            mmReturnJiashuMap.put("shenfenzhenghao",mmShenfenzhenghao);
                            mmReturnJiashuMap.put("shifoujingshang",mmIsZhenbie);
                            mmReturnJiashuMap.put("hebingshu",mmHebing2);
                            mmReturnJiashuMap.put("huanhangfu",mmHuanhang);
                            mmReturnJiashuMap.put("qiyexingtai",mmQiyeXingtai);
                            mmReturnJiashuMap.put("zhenbielist",mmReturnZhenbieList);

                            mmReturnJiashuList.add(mmReturnJiashuMap);
                        }


                    }
                    mmHebing = "ss:MergeDown=\""+hebingshu+"\"";

                    mmRenturnRenyuanMap.put("tianchongge",mmTianchongge);
                    mmRenturnRenyuanMap.put("truename",mmTruename);
                    mmRenturnRenyuanMap.put("jigoumingchengma",mmJigoumingchengma);
                    mmRenturnRenyuanMap.put("buzhibie",mmBuzhibie);
                    mmRenturnRenyuanMap.put("renyuanleibie",mmRenyuanleibie);
                    mmRenturnRenyuanMap.put("chushengnianyue",mmChushengnianyue);

                    if(!"".equals(mmJunxianshijian)){
                        mmRenturnRenyuanMap.put("junxiandengji",mmJunxianleibie+mmJunxiandengji);
                        String mmNewJxtime = mmJunxianshijian.replaceAll("-",".");
                        mmRenturnRenyuanMap.put("junxiandengjishijian",mmNewJxtime);
                    }else{
                        mmRenturnRenyuanMap.put("junxiandengji",mmJunxianleibie+mmJunxiandengji);
                        mmRenturnRenyuanMap.put("junxiandengjishijian","  ");
                    }
                    if(!"".equals(mmZhiwushijian)){
                        mmRenturnRenyuanMap.put("zhiwucengji",mmZhiwucengji);
                        String mmNewZwtime = mmZhiwushijian.replaceAll("-",".");
                        mmRenturnRenyuanMap.put("zhiwucengjishijian",mmNewZwtime);
                    }else{
                        mmRenturnRenyuanMap.put("zhiwucengji",mmZhiwucengji);
                        mmRenturnRenyuanMap.put("zhiwucengjishijian"," ");
                    }

                    mmRenturnRenyuanMap.put("hebingshu",mmHebing);
                    mmRenturnRenyuanMap.put("shifoujingshang",mmShifoujingshang);
                    mmRenturnRenyuanMap.put("xuhao",i+1);
                    mmRenturnRenyuanMap.put("huanhangfu",mmHuanhangfu);
                    mmRenturnRenyuanMap.put("jiashulist",mmReturnJiashuList);
                }
                mmReturnList.add(mmRenturnRenyuanMap);
            }

            mmReturnMap.put("renyuanlist",mmReturnList);
            mmReturnMap.put("deptname",mmDeptName);

            if (userfilepath.endsWith("/")) {
                userfilepath = userfilepath.substring(0, userfilepath.length() - 1);
            }
            SimpleDateFormat mmDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String mmSavePath = userfilepath + "/temp/zhenbieqingkuang/"+mmDateFormat.format(new Date())+"/";
            File mmFold = new File(mmSavePath);
            if (!mmFold.isDirectory() || !mmFold.exists()) {
                mmFold.mkdirs();
            }

            CreateWord.createWord(userfilepath, mmSavePath,  "huizongbiao.xls", mmReturnMap, request,
                    response,"zb","1");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 导出规范汇总表
    public void Guifanhuizong(String ppDeptId,String ppRenyuanLeibieId,String ppJigoumingchengma,String ppPaixu,HttpServletRequest request, HttpServletResponse response){
        try {
            // 存放的文件路径及名称
            String userfilepath = USERFILEPATH;
            String mmDeptName =" ";
            if(ppDeptId!=null && !ppDeptId.equals("")){
                Dept mmDept = ddDeptService.findOne(ppDeptId);
                mmDeptName = mmDept.getDanweiduiwaimingcheng();
            }


            // 获得领导列表
            List<Map<String,Object>> mmList = ddRenyuanMapper.findRenyuanByDeptIdByGuifan(ppDeptId,ppRenyuanLeibieId,ppJigoumingchengma,ppPaixu);
            List<Map<String,Object>> mmReturnList = new ArrayList<>();
            Map<String,Object> mmReturnMap = new HashMap<>();

            // 循环领导信息
            for (int i = 0; i < mmList.size(); i++) {
                Map<String,Object> mmMap = mmList.get(i);
                Map<String,Object> mmRenturnRenyuanMap=new HashMap<>();
                if (mmMap!=null){
                    List<Map<String,Object>> mmReturnJiashuList = new ArrayList<>();

                    String mmRenyuanId = mmMap.get("renyuanid") == null ? "" : mmMap.get("renyuanid").toString();
                    String mmTruename = mmMap.get("truename") == null ? "" : mmMap.get("truename").toString();
                    String mmJigoumingchengma = mmMap.get("jigoumingchengma") == null ? "" : mmMap.get("jigoumingchengma").toString();
                    String mmBuzhibie = mmMap.get("buzhibie") == null ? "" : mmMap.get("buzhibie").toString();
                    String mmRenyuanleibie = mmMap.get("renyuanleibie") == null ? "" : mmMap.get("renyuanleibie").toString();

                    String mmChushengnianyue = mmMap.get("chusheng2") == null ? "" : mmMap.get("chusheng2").toString();
                    String mmJunxianleibie = mmMap.get("junxianleibietext") == null ? "" : mmMap.get("junxianleibietext").toString();
                    String mmJunxiandengji = mmMap.get("junxiandengjitext") == null ? "" : mmMap.get("junxiandengjitext").toString();
                    String mmJunxianshijian = mmMap.get("junxianshijian2") == null ? "" : mmMap.get("junxianshijian2").toString();
                    String mmZhiwucengji = mmMap.get("zhiwucengjitext") == null ? "" : mmMap.get("zhiwucengjitext").toString();
                    String mmZhiwushijian = mmMap.get("zhiwushijian2") == null ? "" : mmMap.get("zhiwushijian2").toString();
                    String mmShifoujingshang = mmMap.get("shifoujingshang") == null ? "" : mmMap.get("shifoujingshang").toString();
                    // 创建 合并数 默认为该干部的规范数一致
                    int hebingshu = 0;
                    int mmZhenbieNum = ddZhenbieQingkuangService.getCountByzhuangtaiAndRenyuanIdByGuifan(mmRenyuanId);
                    if (mmZhenbieNum>0){
                        hebingshu = mmZhenbieNum-1;
                    }

                    String mmHebing = "";


                    // 获得家属列表
                    List<Map<String,Object>> mmJiashuList = ddJiashuService.findByRenyuanidByGuifan(mmRenyuanId);

                    String mmHuanhangfu = ""; // 换行符
                    if (mmJiashuList.size()==0){
                        mmHuanhangfu="</Row><Row>";
                    }

                    String mmTianchongge = "";
                    if (mmJiashuList.size()==0){   // 合并的行数要少一个

                        mmTianchongge = "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s57\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s57\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s57\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s57\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s57\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s57\"><Data ss:Type=\"String\"></Data></Cell>";
                    }

                    if (mmZhenbieNum==0&&mmJiashuList.size()>0){
                        hebingshu = hebingshu-1;
                    }

                    for (int j = 0; j < mmJiashuList.size(); j++) {     // 循环家属信息
                        Map<String,Object> mmJiashuMap = mmJiashuList.get(j);
                        if (mmJiashuMap!=null){
                            List<Map<String,Object>> mmReturnZhenbieList = new ArrayList<>();
                            Map<String,Object> mmReturnJiashuMap = new HashMap<>();

                            String mmJiashuId = mmJiashuMap.get("jiashuid") == null ? "" : mmJiashuMap.get("jiashuid").toString();
                            String mmName = mmJiashuMap.get("name") == null ? "" : mmJiashuMap.get("name").toString();
                            String mmGuanxi = mmJiashuMap.get("guanxitext") == null ? "" : mmJiashuMap.get("guanxitext").toString();
                            String mmShenfenzhenghao = mmJiashuMap.get("shenfenzhenghao") == null ? "" : mmJiashuMap.get("shenfenzhenghao").toString();

                            List<Map<String,Object>> mmZhenbieqingkuangList = ddZhenbieQingkuangService.findByJiashuIdByGuifan(mmJiashuId); // 查询该家属的规范甄别情况

                            // 家属大于1 并且 甄别情况为0 则 合并数加一
                            if(mmJiashuList.size()>1&&mmZhenbieqingkuangList.size()==0){
                                hebingshu= hebingshu+1;
                            }

                            String mmTianchongge2 = "";
                            if (mmZhenbieqingkuangList.size()==0){
                                mmTianchongge2 = "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s57\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                        "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s57\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                        "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s57\"><Data ss:Type=\"String\"></Data></Cell>\n" +
                                        "<Cell ss:MergeDown=\"0\" ss:StyleID=\"s57\"><Data ss:Type=\"String\"></Data></Cell>";
                            }
                            for (int k = 0; k < mmZhenbieqingkuangList.size(); k++) {
                                Map<String,Object> mmJingshangMap = mmZhenbieqingkuangList.get(k);

                                if (mmJingshangMap!=null) {
                                    Map<String,Object> mmReturnZhenbieMap = new HashMap<>();
                                    String mmQiyename = mmJingshangMap.get("qiyename") == null ? "" : mmJingshangMap.get("qiyename").toString(); // 企业名称
                                    String mmBENRENTUICHU = mmJingshangMap.get("benrentuichu") == null ? "" : mmJingshangMap.get("benrentuichu").toString(); // 本人退出
                                    String mmTUICHUJINGSHANG = mmJingshangMap.get("tuichujingshang") == null ? "" : mmJingshangMap.get("tuichujingshang").toString(); // 退出经商企业办
                                    String mmBEIZHU = mmJingshangMap.get("beizhu") == null ? "" : mmJingshangMap.get("beizhu").toString(); // 备注

                                    String[] arr = mmTUICHUJINGSHANG.split(",");
                                    String TUICHUJINGSHANGText="";
                                    for (int m = 0; m < arr.length; m++) {
                                        if("A".equals(arr[m])){
                                            TUICHUJINGSHANGText="注销";
                                        }
                                        if("B".equals(arr[m])){
                                            TUICHUJINGSHANGText+="转让股权（财产份额）";
                                        }
                                        if("C".equals(arr[m])){
                                            TUICHUJINGSHANGText+="变更经营范围";
                                        }
                                        if("D".equals(arr[m])){
                                            TUICHUJINGSHANGText+="调整岗位、不再担任高级职务";
                                        }
                                        if("E".equals(arr[m])){
                                            TUICHUJINGSHANGText+="辞职";
                                        }
                                        if("F".equals(arr[m])){
                                            TUICHUJINGSHANGText+="其他";
                                        }
                                        if(m!=arr.length-1){
                                            TUICHUJINGSHANGText+="，";
                                        }
                                    }

                                    if("1".equals(mmBENRENTUICHU)){
                                        mmBENRENTUICHU="岗位调整";
                                    }
                                    if("2".equals(mmBENRENTUICHU)){
                                        mmBENRENTUICHU="退役";
                                    }
                                    if("3".equals(mmBENRENTUICHU)){
                                        mmBENRENTUICHU="辞职";
                                    }
                                    if("4".equals(mmBENRENTUICHU)){
                                        mmBENRENTUICHU="解除聘用";
                                    }
                                    String mmZhanwei = "";
                                    if (k>0){
                                        mmZhanwei =
                                                "<Cell ss:StyleID=\"s57\"><Data ss:Type=\"String\"></Data></Cell><Cell ss:StyleID=\"s57\"><Data ss:Type=\"String\"></Data></Cell><Cell ss:StyleID=\"s57\"><Data ss:Type=\"String\"></Data></Cell><Cell ss:StyleID=\"s57\"><Data ss:Type=\"String\"></Data></Cell><Cell ss:StyleID=\"s57\"><Data ss:Type=\"String\"></Data></Cell><Cell ss:StyleID=\"s57\"><Data ss:Type=\"String\"></Data></Cell><Cell ss:StyleID=\"s57\"><Data ss:Type=\"String\"></Data></Cell><Cell ss:StyleID=\"s57\"><Data ss:Type=\"String\"></Data></Cell><Cell ss:StyleID=\"s57\"><Data ss:Type=\"String\"></Data></Cell>";
                                    }
                                    String mmHuanhangfu2 = "";
                                    if (!((j-1)==mmJiashuList.size()&&(k-1)==mmZhenbieqingkuangList.size())){
                                        mmHuanhangfu2="</Row><Row>";
                                    }
                                    mmReturnZhenbieMap.put("zhanwei", mmZhanwei);
                                    mmReturnZhenbieMap.put("qiyename", mmQiyename);

                                    mmReturnZhenbieMap.put("benrentuichu", mmBENRENTUICHU);
                                    mmReturnZhenbieMap.put("tuichujingshang", TUICHUJINGSHANGText);

                                    mmReturnZhenbieMap.put("beizhu", mmBEIZHU);

                                    mmReturnZhenbieMap.put("huanhangfu", mmHuanhangfu2);

                                    mmReturnZhenbieList.add(mmReturnZhenbieMap);
                                }
                            }
                            String mmHebing2 = "";
                            if (mmReturnZhenbieList.size()>0){
                                int hebingshu2 = mmReturnZhenbieList.size()-1;
                                mmHebing2 = "ss:MergeDown=\""+hebingshu2+"\"";
                            }

                            String mmHuanhang = "";
                            if (mmReturnZhenbieList.size()==0){
                                mmHuanhang = "</Row><Row>";
                            }
                            String mmZhanwei = "";
                            if (j>0){
                                mmZhanwei="<Cell ss:StyleID=\"s57\"><Data ss:Type=\"String\"></Data></Cell><Cell ss:StyleID=\"s57\"><Data ss:Type=\"String\"></Data></Cell><Cell ss:StyleID=\"s57\"><Data ss:Type=\"String\"></Data></Cell><Cell ss:StyleID=\"s57\"><Data ss:Type=\"String\"></Data></Cell><Cell ss:StyleID=\"s57\"><Data ss:Type=\"String\"></Data></Cell><Cell ss:StyleID=\"s57\"><Data ss:Type=\"String\"></Data></Cell><Cell ss:StyleID=\"s57\"><Data ss:Type=\"String\"></Data></Cell>";
                            }

                            mmReturnJiashuMap.put("zhanwei",mmZhanwei);
                            mmReturnJiashuMap.put("tianchongge",mmTianchongge2);
                            mmReturnJiashuMap.put("name",mmName);
                            mmReturnJiashuMap.put("guanxi",mmGuanxi);
                            mmReturnJiashuMap.put("hebingshu",mmHebing2);
                            mmReturnJiashuMap.put("huanhangfu",mmHuanhang);
                            mmReturnJiashuMap.put("zhenbielist",mmReturnZhenbieList);

                            mmReturnJiashuList.add(mmReturnJiashuMap);
                        }


                    }
                    mmHebing = "ss:MergeDown=\""+hebingshu+"\"";

                    mmRenturnRenyuanMap.put("tianchongge",mmTianchongge);
                    mmRenturnRenyuanMap.put("truename",mmTruename);
                    mmRenturnRenyuanMap.put("jigoumingchengma",mmJigoumingchengma);
                    mmRenturnRenyuanMap.put("buzhibie",mmBuzhibie);
                    mmRenturnRenyuanMap.put("renyuanleibie",mmRenyuanleibie);
                    mmRenturnRenyuanMap.put("chushengnianyue",mmChushengnianyue);

                    if(!"".equals(mmJunxianshijian)){
                        mmRenturnRenyuanMap.put("junxiandengji",mmJunxianleibie+mmJunxiandengji);
                        String mmNewJxtime = mmJunxianshijian.replaceAll("-",".");
                        mmRenturnRenyuanMap.put("junxiandengjishijian",mmNewJxtime);
                    }else{
                        mmRenturnRenyuanMap.put("junxiandengji",mmJunxianleibie+mmJunxiandengji);
                        mmRenturnRenyuanMap.put("junxiandengjishijian","  ");
                    }
                    if(!"".equals(mmZhiwushijian)){
                        mmRenturnRenyuanMap.put("zhiwucengji",mmZhiwucengji);
                        String mmNewZwtime = mmZhiwushijian.replaceAll("-",".");
                        mmRenturnRenyuanMap.put("zhiwucengjishijian",mmNewZwtime);
                    }else{
                        mmRenturnRenyuanMap.put("zhiwucengji",mmZhiwucengji);
                        mmRenturnRenyuanMap.put("zhiwucengjishijian"," ");
                    }


                    mmRenturnRenyuanMap.put("hebingshu",mmHebing);

                    mmRenturnRenyuanMap.put("shifoujingshang",mmShifoujingshang);
                    mmRenturnRenyuanMap.put("xuhao",i+1);
                    mmRenturnRenyuanMap.put("huanhangfu",mmHuanhangfu);
                    mmRenturnRenyuanMap.put("jiashulist",mmReturnJiashuList);
                }
                mmReturnList.add(mmRenturnRenyuanMap);
            }

            mmReturnMap.put("renyuanlist",mmReturnList);
            mmReturnMap.put("deptname",mmDeptName);

            if (userfilepath.endsWith("/")) {
                userfilepath = userfilepath.substring(0, userfilepath.length() - 1);
            }
            SimpleDateFormat mmDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String mmSavePath = userfilepath + "/temp/guifanqingkuang/"+mmDateFormat.format(new Date())+"/";
            File mmFold = new File(mmSavePath);
            if (!mmFold.isDirectory() || !mmFold.exists()) {
                mmFold.mkdirs();
            }

            CreateWord.createWord(userfilepath, mmSavePath,  "guifanhuizongbiao.xls", mmReturnMap, request,
                    response,"gf","1");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void error(String string,HttpServletResponse mmResponse) {
        try {
            mmResponse.setCharacterEncoding("gbk");
            PrintWriter mmOut;
            mmOut = mmResponse.getWriter();
            mmOut.print("<div style='text-align:center;font-size:18px; font-weight:weight;'>"+string+"</div>");
            mmOut.flush();
            mmOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
